app.controller('RtTaskListCtrl', function ($scope, ajax, $modal, toaster) {

	var querier = new Req.GET({ page_index: 1, resource: $scope.resource });
	var updater = new Req.PUT({ resource: $scope.resource });

	//任务状态
	$scope.statusList = [
		{ id: 1, name: "已创建" },
		{ id: 2, name: "已分配" },
		{ id: 3, name: "进行中" },
		{ id: 4, name: "已完成" },
		{ id: 5, name: "已取消" },
		//             { id: 6, name: "已失败" },
		//             { id: 7, name: "已中止" },
		//             { id: 8, name: "未执行" },
	];

	$scope.specialList = [
		{ id: 1, name: "空车返回任务" },
		{ id: 2, name: "非空车返回任务" },
	];

	$scope.reload = $scope.page.loader('task/rt/{pageIndex}/{pageSize}/page');
	$scope.$on('pageIndex', function (event, p) { $scope.reload(p, $scope.queryMap); });
	$scope.reload(1);

	$scope.timeRange = {};
	$scope.queryMap = {};
	$scope.query = function () {
		if ($scope.specialList.selected) {
			if ($scope.specialList.selected.id == 1) {
				$scope.queryMap.special = true;
			} else if ($scope.specialList.selected.id == 2) {
				$scope.queryMap.special = false;
			}
		} else if ($scope.specialList.selected == undefined) {
			delete $scope.queryMap.special;
		}
		if ($scope.statusList.selected != undefined) {
			$scope.queryMap.status = $scope.statusList.selected.id;
		} else if ($scope.statusList.selected == undefined) {
			delete $scope.queryMap.status;
		}
		if ($scope.timeRange.startTime && $scope.timeRange.endTime) {
			var starTime = $scope.timeRange.startTime.pattern("yyyy-MM-dd");
			var endTime = $scope.timeRange.endTime.pattern("yyyy-MM-dd");
			$scope.queryMap.createTime = starTime + "#@" + endTime;
		} else if ($scope.timeRange.startTime == null || $scope.timeRange.endTime == null) {
			delete $scope.queryMap.createTime;
		}
		$scope.reload(1, $scope.queryMap);
	};

	// 新增、更新
	$scope.write = function (size, current_data, submit_type) {
		$scope.current_data = angular.copy(current_data);
		var modalInstance = $modal.open({
			templateUrl: 'template/rent/task/writeModal.html?v=af06b130ff',
			controller: 'RtTaskModalCtrl',
			size: size,
			resolve: {
				param: function () {
					return {
						"data": $scope.current_data,
						"type": submit_type
					};
				}
			}
		});

		modalInstance.result.then(function (param) {
			if (param.type == 'update') {
				updater.fields = param.data;
				updater.id = param.data.id;
				updater.resource = 'task/rt/update';
				$scope.page.mod(updater, $scope.reload);
			}
		});
	};

	$scope.explain = function () {
		$modal.open({
			templateUrl: 'template/rent/task/explain.html?v=ece8dd7bc4',
			controller: 'RtTaskExplainCtrl',
			size: 'lg',
			resolve: {
				param: function () { return {}; }
			}
		});
	};

	$scope.recalling = false;
	$scope.recall = function (task) {
		if ($scope.recalling) return;
		$scope.recalling = true;
		ajax.call(new Req.POST({
			resource: 'task/rt/' + task.id + '/recall',
		})).then(function (result) {
			if (result.code == 200) toaster.pop("success", "成功", "调度任务指派成功！");
			else if (result.code != 423) toaster.pop("error", "失败", result.result);
			$scope.recalling = false;
		}, function () {
			toaster.pop("error", "错误", "与服务器的连接失败！");
			$scope.recalling = false;
		});
	};
});

app.controller('RtTaskExplainCtrl', function ($scope, $modalInstance) {

	$scope.cancel = function () {
		$modalInstance.dismiss('cancel');
	};
});